package com.team.weigou.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.team.weigou.R;
import com.team.weigou.bean.GoodsInfoData;
import com.team.weigou.config.UrlConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

@ContentView(R.layout.activity_goods_layout)
public class GoodsActivity extends Activity{

    @ViewInject(R.id.goods_rebate_info_img)
    private ImageView img;
    @ViewInject(R.id.goods_rebate_info_image)
    private CircleImageView image;
    @ViewInject(R.id.goods_rebate_info_name)
    private TextView name;
    @ViewInject(R.id.goods_rebate_info_backmoney)
    private TextView backmoney;
    @ViewInject(R.id.goods_rebate_info_price)
    private TextView price;
    @ViewInject(R.id.goods_rebate_info_value)
    private TextView value;
    @ViewInject(R.id.goods_rebate_info_time)
    private TextView time;
    @ViewInject(R.id.goods_rebate_info_shop)
    private TextView shop;
    @ViewInject(R.id.goods_rebate_info_wechat)
    private TextView wechat;

    private HttpUtils httpUtils = new HttpUtils();
    private List<GoodsInfoData> list = new ArrayList<>();
    private BitmapUtils bitmapUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initBitmUtils();
        Intent intent = this.getIntent();
        name.setText(intent.getStringExtra("name"));
        price.setText("￥" + intent.getIntExtra("price", 0) + ".0");
        value.setText("￥" + intent.getIntExtra("value", 0) + ".0");
        value.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        backmoney.setText("￥" + intent.getIntExtra("backMoney", 0) + ".00");
        bitmapUtils.display(img, intent.getStringExtra("image"));
        initData();
    }

    /**
     * 初始化BitmapUtils工具类
     */
    private void initBitmUtils() {
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_empty);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_empty);
    }

    private void initData() {
        RequestParams params = getRequestParams();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConfig.REBATE_INFO_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String json = responseInfo.result;
                try {
                    String msg = new JSONObject(json).optString("msg");
                    if ("获取成功".equals(msg)) {
                        JSONObject arr = new JSONObject(json).getJSONObject("data");
                        GoodsInfoData data = JSON.parseObject(arr.toString(), GoodsInfoData.class);
                        shop.setText(data.getName());
                        wechat.setText("微信号:  " + data.getWeixin());
                        bitmapUtils.display(image, data.getImage());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    public RequestParams getRequestParams() {
        Intent intent = getIntent();
        String shopId = intent.getStringExtra("shopId");
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", shopId);
        return params;
    }

    public void back(View v) {
         this.finish();
    }

    public void goShop(View v) {
        Intent intent = new Intent(this, ShopActivity.class);
//        intent.putExtra("price", p);
//        intent.putExtra("value", v);
//        intent.putExtra("name", n);
//        intent.putExtra("shopId", shopId);
//        intent.putExtra("id", data.getId());
        startActivity(intent);
    }

    public void add() {
        //tianjiagouwuche
    }

    public void buy() {
        //tiaozhuangouwuche
    }
}
